﻿(function ($) {

    $(function () {
        var scriptbase = _spPageContextInfo.webServerRelativeUrl + "/_layouts/15/";
            $.getScript(scriptbase + "SP.js", function () {
                $.getScript(scriptbase + "SP.Taxonomy.js", GetPolicyNews('#msPeople #othernews'));
            });
    });

    this.GetPolicyNews = function (idothernews) {
        $('#msPeopleloading').show();
        var currentId = 'msPeople';
        var itemrowlimit = getRowLimitFromContentEditorWP(currentId);
        var siteUrl = window.location.protocol + '//' + window.location.host + '/teams/OurVoiceHN/News/';
        var clientContext = new SP.ClientContext(siteUrl);
        var oList = clientContext.get_web().get_lists().getByTitle('Posts');
        var camlQuery = new SP.CamlQuery();
        camlQuery.set_viewXml('<View><Query><Where><And><Eq><FieldRef Name=\'Is_x0020_Active\' /><Value Type=\'Boolean\'>1</Value></Eq><Eq><FieldRef Name=\'Subject_x0020_Type\' /><Value Type=\'Choice\'>Policy</Value></Eq></And></Where><OrderBy><FieldRef Name=\'Top_x0020_News\' Ascending=\'FALSE\' /><FieldRef Name=\'PublishedDate\' Ascending=\'FALSE\' /></OrderBy></Query><RowLimit>' + itemrowlimit + '</RowLimit></View>');
        var items = oList.getItems(camlQuery);
        clientContext.load(items, "Include(ID,Title,Summary,PublishingPageIcon)");
        clientContext.executeQueryAsync(
            function () {
                $(idothernews).html('');
                var listEnumerator = items.getEnumerator();
                while (listEnumerator.moveNext()) {
                    var item = listEnumerator.get_current();
                    var url = siteUrl + 'Lists/Posts/Post.aspx?ID=' + item.get_item('ID');
                    var othernews = buildOtherNews(item.get_item('PublishingPageIcon'), url, item.get_item('Title'), item.get_item('Summary'));
                    $(idothernews).append(othernews);
                }
                if ($(idothernews).html() == '') {
                    $(idothernews).html('There are no posts in this category.');
                }
                $('#msPeopleloading').hide();
            }, function (sender, args) {
                alert('Processing data. Please wait...');
            }
        );
    };

    this.buildOtherNews = function (imageurl, itemurl, title, content) {
        if (title == null) {
            title = "";
        }

        if (title.length > 50) {
            title = title.substring(0, 50) + '...';
        }

        if (content == null) {
            content = "";
        }

        if (content.length > 180) {
            content = content.substring(0, 180);
        }

        var imgUrl = '';
        if (imageurl != null && $(imageurl).attr('src') != "") {
            imgUrl = $(imageurl).attr('src');
        }
        else {
            imgUrl = '/teams/OurVoiceHN/Style Library/Images/default_image.gif';
        }
        var result = '<div class="newsitem col-sm-12 col-md-12 col-lg-12">';

        result += '<div class="row">';
        result += '<div class="newsleft col-sm-6 col-md-6 col-lg-6">';
        result += '<a href="' + itemurl + '"><img class="img-responsive" src="' + imgUrl + '" /></a>';
        result += '</div>';

        result += '<div class="newsright col-sm-6 col-md-6 col-lg-6">';

        result += '<h3 class="title"><a href="' + itemurl + '">' + title + '</a></h3>';
        result += '<p class="content">' + content + '</p>';
        result += '<a class="viewMore" href="' + itemurl + '">More</a></div>';
        result += '</div>';

        result += '</div>';

        result += '</div>';
        return result;
    };

}).call(this, jQuery);